package com.code;

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DateTimeUtil {
	
	
	public static Date YYYYMMddHHmmss(String timeStr){
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timeStr);
		} catch (ParseException e) {
			e.printStackTrace();
			return new Date();
		}
	}
	public static String nowString(){
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	}
	public static SimpleDateFormat YYYYMdHms(){
		return new SimpleDateFormat("yyyy-M-d H:m:s");
	}
	public static SimpleDateFormat YYYYMd(){
		return new SimpleDateFormat("yyyy-M-d");
	}
	public static SimpleDateFormat Hms(){
		return new SimpleDateFormat("H:m:s");
	}
	public static SimpleDateFormat YYMMddHHmmss(){
		return new SimpleDateFormat("yy-MM-dd HH:mm:ss");
	}
	public static SimpleDateFormat YYMMddHHmmss2(){
		return new SimpleDateFormat("yyMMddHHmmss");
	}
	public static SimpleDateFormat CYYYYMMddHH(){
		return new SimpleDateFormat("yyyy年MM月dd日HH时");
	}
	public static SimpleDateFormat CYYYYMMdd(){
		return new SimpleDateFormat("yyyy年MM月dd日");
	}
	public static SimpleDateFormat CYYYYMM(){
		return new SimpleDateFormat("yyyy年MM月");
	}
	public static SimpleDateFormat CYYYY(){
		return new SimpleDateFormat("yyyy年");
	}
	public static SimpleDateFormat YYYYMMddHHmmssSSS(){
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	}
	public static SimpleDateFormat YYYYMMddHHmmssSSSSSSS(){
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSSS");
	}
	public static SimpleDateFormat YYYYMMddHHmmss(){
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	}
	public static SimpleDateFormat YYYYMMddHHmm(){
		return new SimpleDateFormat("yyyy-MM-dd HH:mm");
	}
	public static SimpleDateFormat YYYYMMddHH(){
		return new SimpleDateFormat("yyyy-MM-dd HH");
	}
	public static SimpleDateFormat YYYYMMddHH_BM(){
		return new SimpleDateFormat("yyyyMMddHH");
	}
	public static SimpleDateFormat YYYYMMdd(){
		return new SimpleDateFormat("yyyy-MM-dd");
	}
	public static SimpleDateFormat HHmm(){
		return new SimpleDateFormat("HH:mm");
	}
	public static SimpleDateFormat HHmmss(){
		return new SimpleDateFormat("HH:mm:ss");
	}
	public static SimpleDateFormat MMdd(){
		return new SimpleDateFormat("MM.dd");
	}
	public static SimpleDateFormat MM_dd(){
		return new SimpleDateFormat("MM-dd");
	}
	public static long daysBetween(Date date1,Date date2){
		return (date2.getTime()-date1.getTime())/86400000;
	}
    public static int daysInMonth(int y,int m){
		
		Calendar calendar1 = Calendar.getInstance();
		calendar1.set(Calendar.HOUR_OF_DAY, 0);
		calendar1.set(Calendar.SECOND, 0);
		calendar1.set(Calendar.MILLISECOND, 0);
		calendar1.set(Calendar.MINUTE, 0);
		calendar1.set(Calendar.DAY_OF_MONTH, 1);
		calendar1.set(Calendar.YEAR, y);
		calendar1.set(Calendar.MONTH, m-1);
        return calendar1.getActualMaximum(Calendar.DAY_OF_MONTH);
	}
     public static int daysInMonth(int m){
		
		Calendar calendar1 = Calendar.getInstance();
		calendar1.set(Calendar.HOUR_OF_DAY, 0);
		calendar1.set(Calendar.SECOND, 0);
		calendar1.set(Calendar.MILLISECOND, 0);
		calendar1.set(Calendar.MINUTE, 0);
		calendar1.set(Calendar.DAY_OF_MONTH, 1);
		calendar1.set(Calendar.MONTH, m-1);
        return calendar1.getActualMaximum(Calendar.DAY_OF_MONTH);
	}
    public static int daysInYear(int y){
		
		Calendar calendar1 = Calendar.getInstance();
		calendar1.set(Calendar.HOUR_OF_DAY, 0);
		calendar1.set(Calendar.SECOND, 0);
		calendar1.set(Calendar.MILLISECOND, 0);
		calendar1.set(Calendar.MINUTE, 0);
		calendar1.set(Calendar.DAY_OF_MONTH, 1);
		calendar1.set(Calendar.YEAR, y);
        return calendar1.getActualMaximum(Calendar.DAY_OF_YEAR);
	}
	public static int daysBetween(int y,String date1,String date2){
		
		Calendar calendar1 = Calendar.getInstance();
		calendar1.set(Calendar.HOUR_OF_DAY, 0);
		calendar1.set(Calendar.SECOND, 0);
		calendar1.set(Calendar.MILLISECOND, 0);
		calendar1.set(Calendar.MINUTE, 0);
		String[] md=date1.split("-");
		calendar1.set(Calendar.YEAR, y);
		if (date1.compareTo(date2)==1){
			calendar1.add(Calendar.YEAR, -1);
		}
		calendar1.set(Calendar.MONTH, Integer.valueOf(md[0])-1);
		if (calendar1.getActualMaximum(Calendar.DAY_OF_MONTH)<Integer.valueOf(md[1])){
			calendar1.set(Calendar.DAY_OF_MONTH, calendar1.getActualMaximum(Calendar.DAY_OF_MONTH));
		}else{
			calendar1.set(Calendar.DAY_OF_MONTH, Integer.valueOf(md[1]));
		}
		
		Calendar calendar2 = Calendar.getInstance();
		calendar2.set(Calendar.HOUR_OF_DAY, 0);
		calendar2.set(Calendar.SECOND, 0);
		calendar2.set(Calendar.MILLISECOND, 0);
		calendar2.set(Calendar.MINUTE, 0);
		md=date2.split("-");
		calendar2.set(Calendar.YEAR, y);
		calendar2.set(Calendar.MONTH, Integer.valueOf(md[0])-1);
		
		if (calendar2.getActualMaximum(Calendar.DAY_OF_MONTH)<Integer.valueOf(md[1])){
			calendar2.set(Calendar.DAY_OF_MONTH, calendar2.getActualMaximum(Calendar.DAY_OF_MONTH));
		}else{
			calendar2.set(Calendar.DAY_OF_MONTH, Integer.valueOf(md[1]));
		}
		calendar2.add(Calendar.DATE, 1);
		return (int) ((calendar2.getTimeInMillis()-calendar1.getTimeInMillis())/86400000);
	}
	public static long hoursBetween(Date date1,Date date2){
		return (date2.getTime()-date1.getTime())/3600000;
	}
	public static long minutesBetween(Date date1,Date date2){
		return (date2.getTime()-date1.getTime())/60000;
	}
	public final static Date DISABLETIME;
	public final static Date ENABLETIME;
	static {
		Calendar c=Calendar.getInstance();
		c.set(1995, 1, 1);
		ENABLETIME=c.getTime();
		c.set(2090, 1,1);
		DISABLETIME=c.getTime();
	}
	public static String getStringNow(){
		return YYYYMMddHHmmss().format(Calendar.getInstance().getTime());
	}
	public static String getStringNowH(){
		return YYYYMMddHH().format(Calendar.getInstance().getTime());
	}	

	public static long DateToInt(String time) {
		Calendar calendar = Calendar.getInstance();
		try {
			if ((time.split(" ")).length == 1)
				time += " 00:00:00";
			calendar.setTime(YYYYMMddHHmmss().parse(time));
			return calendar.getTimeInMillis();// / 60000;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	public static String longToDayString(Format format,long value){
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(value);
		return format.format(calendar.getTime());
		
	}
	public static String getYesterday(String day){
		String yesterday="";
		Calendar calendar = Calendar.getInstance();
		try {
			calendar.setTime(YYYYMMddHHmmss().parse(day));
		} catch (ParseException e) {
			e.printStackTrace();
		}
        calendar.add(Calendar.HOUR_OF_DAY,-24);
	    yesterday= YYYYMMddHHmmss().format(calendar.getTime());

		return yesterday;
	}
	public static String yesterdayString(String today){
		String yesterday="";
		Calendar calendar = Calendar.getInstance();
		try {
			calendar.setTime(YYYYMMdd().parse(today));
		} catch (ParseException e) {
			e.printStackTrace();
		}
        calendar.add(Calendar.DATE,-1);
	    yesterday= YYYYMMdd().format(calendar.getTime());

		return yesterday;
	}
	public static String tomorrowString(String today){
		String tomorrow="";
		Calendar calendar = Calendar.getInstance();
		try {
			calendar.setTime(YYYYMMdd().parse(today));
		} catch (ParseException e) {
			e.printStackTrace();
		}
        calendar.add(Calendar.DATE,1);
        tomorrow= YYYYMMdd().format(calendar.getTime());

		return tomorrow;
	}
	public static Date toDate(String dateString){
		try {
			return YYYYMMdd().parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static Date toDateTime(String dateString){
		try {
			return YYYYMMddHHmmss().parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static Date toDateTimeM(String dateString){
		try {
			return YYYYMMddHHmm().parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static String toDate(Date date){
		return YYYYMMdd().format(date);
	}
	public static String toDateTime(Date date){
		return YYYYMMddHHmmss().format(date);
	}
	public static String toHourString(Date date){
        return HHmmss().format(date);
	}
	public static String todayString(){
        return YYYYMMdd().format(new Date());
	}
	public static String getYesterdayH(String day){
		String yesterday="";
		Calendar calendar = Calendar.getInstance();
		try {
			calendar.setTime(YYYYMMdd().parse(day));
		} catch (ParseException e) {
			e.printStackTrace();
		}
        calendar.add(Calendar.HOUR_OF_DAY,-24);
	    yesterday= YYYYMMdd().format(calendar.getTime());

		return yesterday;
	}
	public static Date nowHour(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		//calendar.add(Calendar.HOUR_OF_DAY, 1);
		return calendar.getTime();
	}
	public static Date nowDate(int hour){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		//calendar.add(Calendar.HOUR_OF_DAY, 1);
		return calendar.getTime();
	}
	public static Date hourDate(Date date,int hour){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		//calendar.add(Calendar.HOUR_OF_DAY, 1);
		return calendar.getTime();
	}
	public static Date monthStart(Date date,int hour){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		//calendar.add(Calendar.HOUR_OF_DAY, 1);
		return calendar.getTime();
	}
	public static Date dayStart(Date date){
		return dayStart(date,0);
	}
	public static Date dayStart(Date date,int hour){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		//calendar.add(Calendar.HOUR_OF_DAY, 1);
		return calendar.getTime();
	}
	public static Date monthStart(Date date){
		return monthStart(date,0);
	}
	public static int daysOfMonth(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}
	public static int days(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}
	public static int months(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH)+1;
	}
	//public static Calendar calendar = Calendar.getInstance();
	
	public static Date dateAdd(Date time,int sj, String flag){
		Calendar calendar = Calendar.getInstance();
	    calendar.setTime(time);
	    switch (flag) {
			case "YEAR":
				calendar.add(Calendar.YEAR, sj);
				break;
			case "MONTH":
				calendar.add(Calendar.MONTH, sj);
				break;
			case "DAY":
				calendar.add(Calendar.DATE, sj);
			case "HOUR":
				calendar.add(Calendar.HOUR, sj);
			case "MINUTE":
				calendar.add(Calendar.MINUTE, sj);
			case "SECOND":
				calendar.add(Calendar.SECOND, sj);
		}
	    return calendar.getTime();
	}
	public static Date dateAdd(Date time,int hours){
		Calendar calendar = Calendar.getInstance();
	    calendar.setTime(time);
	    int millisecond = hours*60*60*1000;
	    calendar.add(Calendar.MILLISECOND, millisecond);
	    return calendar.getTime();
	}
	public static Date dateAdd(Date time,double hours){
		Calendar calendar = Calendar.getInstance();
	    calendar.setTime(time);
	    int millisecond = (int)(hours*60*60*1000);
	    calendar.add(Calendar.MILLISECOND, millisecond);
	    Date etime=calendar.getTime();
		return etime;
	}
	public static Date dateAddBySECOND(Date time,int second){
		Calendar calendar = Calendar.getInstance();
	    calendar.setTime(time);
	    calendar.add(Calendar.SECOND, second);
	    return calendar.getTime();
	}
	public static Date dateAddBySECOND(int second){
		Calendar calendar = Calendar.getInstance();
	    calendar.add(Calendar.SECOND, second);
	    return calendar.getTime();
	}
	public static Date dateAddByMILLISECOND(Date time,int millisecond){
		Calendar calendar = Calendar.getInstance();
	    calendar.setTime(time);
	    calendar.add(Calendar.MILLISECOND, millisecond);
	    return calendar.getTime();
	}
	public static Date dateAddByMinute(Date time,int minute){
		Calendar calendar = Calendar.getInstance();
	    calendar.setTime(time);
	    calendar.add(Calendar.MINUTE, minute);
	    return calendar.getTime();
	}
	public static Date dateAddByMinute(Date time,double minute){
		Calendar calendar = Calendar.getInstance();
	    calendar.setTime(time);
	    int millisecond = (int)(minute*60*1000);
	    calendar.add(Calendar.MILLISECOND, millisecond);
	    return calendar.getTime();
	}
	public static Date dateAddByDay(Date time,int day){
		Calendar calendar = Calendar.getInstance();
	    calendar.setTime(time);
	    calendar.add(Calendar.DATE, day);
	    return calendar.getTime();
	}
	public static Date dateAddByDayNotHour(Date time,int day){
		String timeStr = DateTimeUtil.YYYYMMdd().format(time);
		Date newTime;
		try {
			newTime = DateTimeUtil.YYYYMMdd().parse(timeStr);
		} catch (ParseException e) {
			newTime = new Date();
		}
		return DateTimeUtil.dateAddByDay(newTime, day);
	}
	public static Date yesterday(Date time){
		return dateAddByDay(time,-1);
	}
	public static Date tomorrow(Date time){
		return dateAddByDay(time,1);
	}
	public static Date dateAddByMonth(Date time,int month){
		Calendar calendar = Calendar.getInstance();
	    calendar.setTime(time);
	    calendar.add(Calendar.MONTH, month);
	    return calendar.getTime();
	}
	public static Date dateAddByYear(Date time,int year){
		Calendar calendar = Calendar.getInstance();
	    calendar.setTime(time);
	    calendar.add(Calendar.YEAR, year);
	    return calendar.getTime();
	}
	public static Date hourStartTimeBefore(String base,String gap,Date time){
    	long repeatInterval= repeatInterval(gap);
    	Calendar cl=Calendar.getInstance();
    	cl.setTime(time);
    	//Date now=cl.getTime();
    	String[] times=base.split(":");
    	int hour=Integer.valueOf(times[0]);
  	    int minute=Integer.valueOf(times[1]);
    	if (cl.get(Calendar.HOUR_OF_DAY)<hour){
    		cl.add(Calendar.DATE, -1);
    	}
    	cl.set(Calendar.HOUR_OF_DAY, hour);
    	cl.set(Calendar.MINUTE, minute);
    	cl.set(Calendar.SECOND, 0);
    	cl.set(Calendar.MILLISECOND, 0);
    	int head=(int) ((time.getTime()-cl.getTime().getTime())%repeatInterval);
        cl.setTime(time);
        cl.add(Calendar.MILLISECOND, -head);
    	return cl.getTime();
    }
	public static Date hourStartTimeAfter(String base,String gap,Date time){
    	long repeatInterval= repeatInterval(gap);
    	Calendar cl=Calendar.getInstance();
    	cl.setTime(time);
    	//Date now=cl.getTime();
    	String[] times=base.split(":");
    	int hour=Integer.valueOf(times[0]);
  	    int minute=Integer.valueOf(times[1]);
    	if (cl.get(Calendar.HOUR_OF_DAY)<hour){
    		cl.add(Calendar.DATE, -1);
    	}
    	cl.set(Calendar.HOUR_OF_DAY, hour);
    	cl.set(Calendar.MINUTE, minute);
    	cl.set(Calendar.SECOND, 0);
    	cl.set(Calendar.MILLISECOND, 0);
    	int tail=(int) (repeatInterval-(time.getTime()-cl.getTime().getTime())%repeatInterval);
        cl.setTime(time);
        cl.add(Calendar.MILLISECOND, tail);
    	return cl.getTime();
    }
	public static long repeatInterval(String gap){
    	String[] times=gap.split(":");
    	int hour=Integer.valueOf(times[0]);
    	int minute=Integer.valueOf(times[1]);
    	int second=times.length==2?0:Integer.valueOf(times[2]);
    	return 1000*(hour*3600+minute*60+second);
    }
	
	public static Date endTime(String qsjz) {
    	Calendar cl=Calendar.getInstance();
    	String[] times=qsjz.split(":");
    	int hour=Integer.valueOf(times[0]);
  	    int minute=Integer.valueOf(times[1]);
    	if (cl.get(Calendar.HOUR_OF_DAY)<hour){
    		cl.add(Calendar.DATE, -1);
    	}
    	cl.set(Calendar.YEAR,9999);
    	cl.set(Calendar.HOUR_OF_DAY, hour);
    	cl.set(Calendar.MINUTE, minute);
    	cl.set(Calendar.SECOND, 0);
    	cl.set(Calendar.MILLISECOND, 0);
    	return cl.getTime();
	}
	
	public static Date startTime(String qsjz,String qsjx,int gap){
    	long repeatInterval= repeatInterval(qsjx);
    	Calendar cl=Calendar.getInstance();
    	Date now=cl.getTime();
    	String[] times=qsjz.split(":");
    	int hour=Integer.valueOf(times[0]);
  	    int minute=Integer.valueOf(times[1]);
    	if (cl.get(Calendar.HOUR_OF_DAY)<hour){
    		cl.add(Calendar.DATE, -1);
    	}
    	cl.set(Calendar.HOUR_OF_DAY, hour);
    	cl.set(Calendar.MINUTE, minute);
    	cl.set(Calendar.SECOND, 0);
    	cl.set(Calendar.MILLISECOND, 0);
    	int tail=(int) (repeatInterval-(now.getTime()-cl.getTime().getTime())%repeatInterval);
        cl.setTime(now);
        cl.add(Calendar.MILLISECOND, tail);
        cl.add(Calendar.MILLISECOND, gap);
    	return cl.getTime();
    }
	

	public static Date startTime(String qsjg, Date endTime,int roundCount){
    	Calendar cal=Calendar.getInstance();
    	cal.setTime(endTime);
    	cal.add(Calendar.MILLISECOND, -(int)(roundCount*repeatInterval(qsjg)));
    	return cal.getTime();
    }
	public static int hours(Date date){
		Calendar cal=Calendar.getInstance();
    	cal.setTime(date);
    	return cal.get(Calendar.HOUR_OF_DAY);
	}
	public static int years(Date date){
		Calendar cal=Calendar.getInstance();
    	cal.setTime(date);
    	return cal.get(Calendar.YEAR);
	}
	
	public static Object dataClass(Date date,String dataClass){
		Calendar cal=Calendar.getInstance();
    	cal.setTime(date);
		switch (dataClass){
		case "时间_年":return cal.get(Calendar.YEAR);
		case "时间_月":return cal.get(Calendar.MONTH)+1;
		case "时间_日":return cal.get(Calendar.DAY_OF_MONTH);
		case "时间_时刻":return date;
		case "时间_秒":return date.getTime()/1000;
		}
		return date;
	}
	public static Date today(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		//calendar.add(Calendar.HOUR_OF_DAY, 1);
		return calendar.getTime();
	}
	public static Date tomorrow(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.add(Calendar.DATE, 1);
		//calendar.add(Calendar.HOUR_OF_DAY, 1);
		return calendar.getTime();
	}
	public static Date sqlToday(){
		return new java.sql.Date(today().getTime());
	}
	public static Date sqlTomorrow(){
		return new java.sql.Date(tomorrow().getTime());
	}
	public static Date newSqlDate(){
		return new java.sql.Date(new Date().getTime());
	}
	public static Date toSqlDate(Date date){
		return new java.sql.Date(date.getTime());
	}
	public static Date getMinMonthDate(Date date) throws ParseException{  
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));  
        return calendar.getTime();  
    }  


	public static Date getMaxMonthDate(Date date) throws ParseException{  
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));  
        return calendar.getTime();  
	} 

	public static Map<String,Object> stimeAndEtime(Date ctime,int hourBase,String type){
		Map<String,Object> params=new HashMap<String,Object>();
		Calendar cal=Calendar.getInstance();
		cal.setTime(ctime);
		int hour=cal.get(Calendar.HOUR_OF_DAY);
		int dayAdd=0;
		if (hourBase<0){
			if (hour>(24+hourBase)){
			  dayAdd=1;
			}
		}else if (hour<=hourBase){
			dayAdd=-1;
		}
		cal.add(Calendar.DATE, dayAdd);
		switch (type){

		case "计算年份":{
			cal.set(Calendar.MONTH, 0);
			cal.set(Calendar.DAY_OF_MONTH, 1);
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.SECOND, 0);
			cal.set(Calendar.MILLISECOND, 0);
			cal.set(Calendar.MINUTE, 0);
			
			cal.add(Calendar.HOUR_OF_DAY, hourBase);
			params.put("stime", cal.getTime());
			params.put("Y", cal.get(Calendar.YEAR));
			cal.add(Calendar.YEAR, 1);
			params.put("etime", cal.getTime());
			
			break;
		}
		case "计算月份":{
			cal.set(Calendar.DAY_OF_MONTH, 1);
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.SECOND, 0);
			cal.set(Calendar.MILLISECOND, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.add(Calendar.HOUR_OF_DAY, hourBase);
			params.put("stime", cal.getTime());
			params.put("Y", cal.get(Calendar.YEAR));
			params.put("M", cal.get(Calendar.MONTH)+1);
			cal.add(Calendar.MONTH, 1);
			params.put("etime", cal.getTime());
			cal.setTime(ctime);
			cal.set(Calendar.MONTH, 0);
			cal.set(Calendar.DAY_OF_MONTH, 1);
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.SECOND, 0);
			cal.set(Calendar.MILLISECOND, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.add(Calendar.DAY_OF_MONTH, dayAdd);
			cal.add(Calendar.HOUR_OF_DAY, hourBase);
			params.put("ystime", cal.getTime());
			break;
		}
		case "计算日期":{
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.SECOND, 0);
			cal.set(Calendar.MILLISECOND, 0);
			cal.set(Calendar.MINUTE, 0);
			//params.put("DT", cal.getTime());
			cal.add(Calendar.HOUR_OF_DAY, hourBase);
			params.put("stime", cal.getTime());
			params.put("DT", cal.getTime());
			cal.add(Calendar.DAY_OF_MONTH, 1);
			params.put("etime", cal.getTime());
			break;
		}
		case "计算旬":{
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.SECOND, 0);
			cal.set(Calendar.MILLISECOND, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.add(Calendar.HOUR_OF_DAY, hourBase);
			params.put("Y", cal.get(Calendar.YEAR));
			params.put("M", cal.get(Calendar.MONTH)+1);
			if (cal.get(Calendar.DAY_OF_MONTH)>20){
				cal.set(Calendar.DAY_OF_MONTH, 21);
				params.put("DS", "下旬");
				params.put("stime", cal.getTime());
				cal.set(Calendar.DAY_OF_MONTH, 1);
				cal.add(Calendar.MONTH, 1);
				params.put("etime", cal.getTime());
			}else if (cal.get(Calendar.DAY_OF_MONTH)>10){
				cal.set(Calendar.DAY_OF_MONTH, 11);
				params.put("DS", "中旬");
				params.put("stime", cal.getTime());
				cal.set(Calendar.DAY_OF_MONTH, 21);
				params.put("etime", cal.getTime());
			}else{
				cal.set(Calendar.DAY_OF_MONTH, 1);
				params.put("DS", "上旬");
				params.put("stime", cal.getTime());
				cal.set(Calendar.DAY_OF_MONTH, 11);
				params.put("etime", cal.getTime());
			}
			break;
		}
		}
		return params;
	}
	public static Map<String,Object> currentGj(List<Map<String,Object>> gjs,Date ctime){
		String d=DateTimeUtil.MM_dd().format(ctime);
		for (Map<String,Object> tgj:gjs){
			if (((String)tgj.get("灌季结束日期")).compareTo((String)tgj.get("灌季起始日期"))==1){
				if (d.compareTo((String)tgj.get("灌季起始日期"))!=-1&&d.compareTo((String)tgj.get("灌季结束日期"))!=1){
					return tgj;
				}
			}else{
				if (d.compareTo((String)tgj.get("灌季起始日期"))!=-1||d.compareTo((String)tgj.get("灌季结束日期"))!=1){
					return tgj;
				}
			}
		}
		return null;
	}
	public static int[][] waterYearMonth(){
		return waterYearMonth(waterYear()[0],-11);
	}
	public static int[][] waterYearMonth(int year){
		return waterYearMonth(year,-11);
	}
	public static int[][] waterYearMonth(int year,int smonth){
		int[][] yms=new int[12][2];
		if (smonth<0){
			year--;
			smonth*=-1;
		}
		for (int i=0;i<12;i++){
			yms[i][0]=year;
			yms[i][1]=smonth;
			if (smonth==12){
				year++;
				smonth=1;
			}else{
				smonth++;
			}
		}
		return yms;
	}
	public static int[] waterYear(){
		Calendar cal=Calendar.getInstance();
		return waterYear(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH)+1,-11);
	}
	public static int[] waterYear(int year){
		return waterYear(year,1,-11);
	}
	public static int[] waterYear(int year,int month){
		return waterYear(year,month,-11);
	}
	public static int[] waterYear(int year,int month,int smonth){
		int [] vs=new int[5];
		if (smonth==1){
			vs[0]=year;
			vs[1]=year;
			vs[2]=smonth;
			vs[3]=year;
			vs[4]=12;
		}else{
			if (smonth<0){
				smonth*=-1;
				if (month<smonth){
					vs[0]=year;
					vs[1]=year-1;
					vs[2]=smonth;
					vs[3]=year;
					vs[4]=smonth-1;
				}else{
					vs[0]=year+1;
					vs[1]=year;
					vs[2]=smonth;
					vs[3]=year+1;
					vs[4]=smonth-1;
				}
			}else{
				if (month<smonth){
					vs[0]=year-1;
					vs[1]=year-1;
					vs[2]=smonth;
					vs[3]=year;
					vs[4]=smonth-1;
				}else{
					vs[0]=year;
					vs[1]=year;
					vs[2]=smonth;
					vs[3]=year+1;
					vs[4]=smonth-1;
				}
			}
		}
		return vs;
	}
	public static void main(String[] args) throws ParseException{
		double[][] x={{840.0},{855.0},{870.0},{885.0},{900.0},};
    	double[] y={0.6502,0.6608,0.679,0.6734,0.7041};
		System.out.println(YYYYMMddHHmmssSSS().parse("2014-08-22 06:08:33.1870000"));
		System.out.println(YYYYMMddHHmmssSSS().parse("2014-08-22 07:01:02.9370000"));
		System.out.println(YYYYMMddHHmmssSSS().parse("2014-08-22 08:09:07.4830000"));
		System.out.println(YYYYMMddHHmmssSSS().parse("2014-08-22 06:08:33.187"));
		System.out.println(YYYYMMddHHmmssSSS().parse("2014-08-22 07:01:02.937"));
		System.out.println(YYYYMMddHHmmssSSS().parse("2014-08-22 08:09:07.483"));
		
		
	}
}
